/**
 * Created by yangxianming on 2019/10/22.
 */
function initializeData(){

}
//
//接口定义 都是以api开头 所需参数都通过方法传入
//
//获取配置数据
function apiGetData(dofun){
    var data = {
        "action":"getData"
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
                if (r["apiStatus"] == "1"){
                    dofun(r["result"]);
                    //return r["result"]
                }
            }
        }
    };
    xmlHttp1.open("POST", "/configuration/dataManager", true);
    xmlHttp1.send(JSON.stringify(data));
}

//获取项目列表
function apiGetProject(dofun){
    var data = {
        "action":"getProject"
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
                if (r["apiStatus"] == "1"){
                    dofun(r["result"]);
                    //return r["result"]
                }
            }
        }
    };
    xmlHttp1.open("POST", "/configuration/projectManager", true);
    xmlHttp1.send(JSON.stringify(data));
}
//添加项目
function apiAddProject(dofun, projectName){
    var data = {
        "action":"addProject",
        "projectName":projectName
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"]);
                }
                if (r["apiStatus"] == "1"){
                    alert(r["result"]);
                    dofun();
                    //return r["result"]
                }
            }
        }
    };
    xmlHttp1.open("POST", "/configuration/projectManager", true);
    xmlHttp1.send(JSON.stringify(data));
}
//删除项目
function apiDelProject(dofun, projectName){
    var data = {
        "action":"delProject",
        "projectName":projectName
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
                if (r["apiStatus"] == "1"){
                    alert(r["result"]);
                    dofun();
                }
            }
        }
    };
    xmlHttp1.open("POST", "/configuration/projectManager", true);
    xmlHttp1.send(JSON.stringify(data));
}

//
//页面渲染 都是以draw开头
//
function drawProject(data){
    var projectContent = document.getElementById("project");
    projectContent.innerText = "";
    var ul = document.createElement("ul");
    ul.setAttribute("style", "margin-left: 10px; margin-top: 10px");
    projectContent.appendChild(ul);

    for(var ind = 0; ind < data.length; ind++){
        var innerLi = document.createElement("li");
        var div1 = document.createElement("div");
        var div2 = document.createElement("div");

        //div1.setAttribute("style", "width: 100px");
        div1.innerText = data[ind];
        div2.setAttribute("style", "width: 20px");
        div2.innerText = "-";
        div2.onclick = function(){delProject(this);};
        innerLi.appendChild(div1);
        innerLi.appendChild(div2);
        ul.appendChild(innerLi);
    }

    var lastLi = document.createElement("li");
    var lastInput = document.createElement("input");
    var lastDiv = document.createElement("div");
    lastInput.setAttribute("style", "height: 20px;  margin: 0 !important;  width: 90px !important;");
    lastInput.setAttribute("type", "text;");
    lastDiv.setAttribute("style", "width: 20px;margin-left: 10px");
    lastDiv.innerText = "+";
    lastDiv.onclick = function(){addProject(this);};
    lastLi.appendChild(lastInput);
    lastLi.appendChild(lastDiv);

    ul.appendChild(lastLi);
}

function drawGetData(data){
    var tableInfo = document.getElementById("infoTable").getElementsByTagName("tbody")[0];
    //清空所有行
    var firstTr = tableInfo.getElementsByTagName("tr")[0];
    tableInfo.innerText = "";
    tableInfo.appendChild(firstTr);

    for(var i = 0; i < data.length; i++){
        var projectName = data[i][0];
        var describe = data[i][1];
        var key = data[i][2];
        var value = data[i][3];
        var td1 = document.createElement("td");
        var td2 = document.createElement("td");
        var td3 = document.createElement("td");
        var td4 = document.createElement("td");
        td1.innerText = projectName;
        td2.innerText = describe;
        td3.innerText = key;
        td4.innerText = value;
        var tr = document.createElement("tr");
        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);
        tr.appendChild(td4);
        tableInfo.appendChild(tr);
    }

}

//
//页面调用 数据获取，接口层与渲染层的连接
//
function projectList(){
    //var td = ["添加项目2","福员外1","福员外","口福商城","口福"];
    apiGetProject(drawProject);
}

function addProject(ob){
    var projectName = ob.previousSibling.value;
    apiAddProject(projectList, projectName);

}

function delProject(ob){
    var projectName = ob.previousSibling.innerText;
    apiDelProject(projectList, projectName);
}

//数据配置
function dataList(){
    apiGetData(drawGetData)
}